###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# Libraries/Templates/Tools.pm -> Tools template library                      #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package Tools;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Standard.pm.pl";

use strict;

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless ($self, $class);
}

sub DESTROY {}

###############################################################################
# MapArray subroutine
sub MapArray {
  my $self = shift;
  my ($Template, $Input) = @_;
  
  my @Return = &Standard::MapArray($Template, $Input);
  
  return \@Return;
}

###############################################################################
# SelfURL subroutine
sub SelfURL {
  my $self = shift;
  my %in = (ref($_[0]) eq "HASH" ? (%{$_[0]}) : (@_));
  
  my %Params;
  
  foreach my $key ($SL::CGI->param()) {
    $Params{ $key } = [$SL::CGI->param($key)];
  }
  
  foreach my $key (keys %in) {
    if (ref($in{$key}) eq "ARRAY") {
      $Params{ $key } = $in{$key};
    } else {
      if ($in{$key} eq "") {
        delete($Params{ $key });
      } else {
        $Params{ $key } = [$in{$key}];
      }
    }
  }
  
  my @Return;
  foreach my $key (keys %Params) {
    foreach my $value (@{ $Params{$key} }) {
      push(@Return, $key."=".$SL::CGI->escape($value));
    }
  }
  
  return $SYSTEM->{'SCRIPT_URL'}."?".join("&", @Return);
}

###############################################################################
# SplitColumns subroutine
sub SplitColumns {
  my $self = shift;
  my ($Items, $Columns, $Column) = @_;

  my @Return;
  
  for (my $h = $Column - 1; $h <= $#$Items; $h = $h + $Columns) {
    push(@Return, $Items->[$h]);
  }
  
  return \@Return;
}

1;